<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use think\Request;
use think\response\Json;
use think\Url;
class Index extends Controller
{
    public function ss(){
        print_r("乐刻健身群的小伙伴们新年快乐，新的一年预祝各位拥有完美的身材！ -- L.E.A.F");
    }
    public function org(){
        print_r("");
    }
    public function index()
    {
       return $this->fetch('tjh_html_homepage/tjh_homepage');
    }
    public function tjh_setting(){
        return $this->fetch("tjh_html_i/tjh_setting");
    }
    public function tjh_suggestions(){
        return $this->fetch("tjh_html_i/tjh_suggestions");
    }
    public function tjh_scancodepayment(){
        return $this->fetch("tjh_html_i/tjh_scancodepayment");
    }
    public function tjh_homepage(){
        return $this->fetch('tjh_html_homepage/tjh_homepage');
    }
    public function tjh_assortment(){
        return $this->fetch("tjh_html_classification/tjh_assortment");
    }
    public function tjh_login(){
        return $this->fetch("tjh_html_i/tjh_login");
    }
    public function tjh_ShoppingCart(){
        return $this->fetch("tjh_html_ShoppingCart/tjh_ShoppingCart");
    }
    public function tjh_UserCenter(){
        return $this->fetch("tjh_html_i/tjh_UserCenter");
    }
    public function tjhyimeishop(){
        return $this->fetch("tjh_html_homepage/tjhyimeishop");
    }
    public function tjh_productshop(){
        return $this->fetch("tjh_html_homepage/tjh_productshop");
    }
    public function tjh_netloanservice01(){
        return $this->fetch("tjh_html_homepage/tjh_netloanservice01");
    }
    public function tjh_famous(){
        return $this->fetch("tjh_html_homepage/tjh_famous");
    }
    public function tjh_shopdetails_shop(){
        return $this->fetch("tjh_html_homepage/tjh_shopdetails_shop");
    }
    public function tjh_OrderDetails(){
        return $this->fetch("tjh_html_i/tjh_OrderDetails");
    }
    public function tjh_evaluate(){
        return $this->fetch("tjh_html_i/tjh_evaluate");
    }
    public function tjh_Submit_Order(){
        return $this->fetch("tjh_html_i/tjh_Submit-Order");
    }
    public function tjh_Commoditylogistics(){
        return $this->fetch("tjh_html_i/tjh_Commoditylogistics");
    }
    public function tjh_Address_Edit(){
        return $this->fetch("tjh_html_i/tjh_Address_Edit");
    }
    public function tjh_Cosmetology(){
        return $this->fetch("tjh_html_homepage/tjh_Cosmetology");
    }
    public function tjh_myOrders(){
        return $this->fetch("tjh_html_i/tjh_myOrders");
    }
    public function tjh_supplierList(){
        return $this->fetch("tjh_html_i/tjh_supplierList");
    }
    public function tjh_supplierList_Offlinestore(){
        $money = Db::name('unline_price')->select();
        $this->assign('money',$money);
        return $this->fetch("tjh_html_i/tjh_supplierList-Offlinestore");
    }
    public function tjh_WithdrawCash(){
        return $this->fetch("tjh_html_i/tjh_WithdrawCash");
    }
    public function tjh_SuccessfulOfflineStore(){
        return $this->fetch("tjh_html_i/tjh_SuccessfulOfflineStore");
    }
    public function tjh_SuccessfulApplication(){
        return $this->fetch("tjh_html_i/tjh_SuccessfulApplication");
    }
    public function tjh_activity(){
        return $this->fetch("tjh_html_homepage/tjh_activity");
    }
    public function tjh_tjhzhuqu(){
        return $this->fetch("tjh_html_homepage/tjh_tjhzhuqu");
    }
    public function tjh_medical(){
        return $this->fetch("tjh_html_homepage/tjh_medical");

    }
    public function tjh_shopdetails_details(){
        return $this->fetch("tjh_html_homepage/tjh_shopdetails_details");
    }
    public function sss(){
        return $this->fetch();
    }

    public function copy_goods(){
        $where = [
            'mc_id' => 6,
            'm_id' => 1,
            'c_id' => 9,
            'is_delete' => 0
        ];
        $goods = Db::name("goods")->where($where)->find();
        unset($goods['id']);
        for($i = 0; $i < 50; $i ++){
            $res = Db::name("goods")->insertGetId($goods);
        }
    }
    /**
    商城轮播图
     * @param m_id 商城id
     */
    public function index_shuffling($m_id = 0){
        empty($m_id) && $this->error("获取失败");
        $where_shuffling = [
            'm_id' => $m_id,
            'is_delete' => 0
        ];
        $shuffling = Db::name('shuffling')->where($where_shuffling)->limit(4)->select();
        return format_return(1,$shuffling,"获取数据成功");
    }
    /**
    医美商城轮播图下
     */
    public function medical_undershuffling(){
        $where = [
            'm_id' => 1,
            'is_delete' => 0,
            'type' => 0
        ];
        $img = Db::name("mall_index")->where($where)->limit(2)->select();
        return format_return(1,$img,"获取数据成功");
    }
    /**
    客服
     * @param s_id
     */
    public function kefu($s_id = 3){
        $kefu = Db::name('kefu')->where(array('s_id'=>$s_id))->find();
        if(empty($kefu)){
            return format_return(0,'',"链接获取失败!");
        }else{
            return format_return(1,$kefu['http'],"链接获取成功");
        }
    }
    /**
    医美商城分区,必须分区下面
     * @param m_id 商城id
     */
    public function index_partition($m_id = 0){
        empty($m_id) && $this->error("获取失败");
        $where = [
            'parent_id' => $m_id,
            'is_delete' => 0,
            'is_fixed' => 0
        ];
        $mall = Db::name("mall")->where($where)->select();
        return format_return(1,$mall,"获取数据成功");
    }

    /**
    商城首页，拼接四张活动图
     * @param m_id 商城id
     */
    public function index_mosaic($m_id = 0){
        empty($m_id) && $this->error("获取失败");
        $where = [
            'm_id' => $m_id,
            'is_delete' => 0,
            'type' => 1
        ];
        $img = Db::name("mall_index")->where($where)->limit(4)->select();
        return format_return(1,$img,"获取数据成功");
    }
    /**
    商城实惠优品
     * @param m_id 商城id
     */
    public function index_benefit($m_id = 0){
        empty($m_id) && $this->error("获取失败");
        $where = [
            'm_id' => $m_id,
            'is_delete' => 0,
            'type' => 2
        ];
        $img = Db::name("mall_index")->where($where)->select();
        return format_return(1,$img,"获取数据成功");
    }

    /**
    首页精选
     * @param m_id 商城id
     * @param page 分页
     */
    public function index_select($page = 0,$m_id = 0){
        empty($m_id) && $this->error("获取失败");
        $where_goods = [
            'is_delete' => 0,
            'is_selected' => 1,
            'm_id' => $m_id,
            'status' => 1
        ];
        $field = [
            'id',
            'm_id',
            'mc_id',
            'cover',
            'subject',
            'introduction',
            'price',
            'integral',
            'is_selected'
        ];
        $start_goods = 4*$page;
        $goods = Db::name("goods")->where($where_goods)->limit($start_goods,4)->field($field)->order('sell_num desc')->select();
        $where_img = [
            'm_id' => $m_id,
            'is_delete' => 0,
            'type' => 3
        ];
        $img = Db::name("mall_index")->where($where_img)->limit($page,1)->select();
        $data['goods'] = $goods;
        $data['selected'] = $img;
       // print_r($data);die;
        if(empty($goods) && empty($img)){
            return format_return(0,'','数据获取失败');
        }else{
            return format_return(1,$data,"获取数据成功");
        }
    }

    /**
    分类页面
     */
    public function category(){
        $where = [
            'is_delete' => 0
        ];
        $category = Db::name("category")->where($where)->order('sort desc')->select();
        $goods = '';
        if(!empty($category)){
            $where_goods = [
                'is_delete' => 0,
                'c_id' => $category[0]['id'],
                'status' => 1
            ];
            $field = [
                'subject',
                'id'
            ];
            $goods = Db::name("goods")->where($where_goods)->field($field)->limit(15)->select();
        }
        $data['category'] = $category;
        $data['goods'] = $goods;
        return format_return(1,$data,"数据获取成功");
    }
    /**
    分区
     */
    public function index_mall(){
        $where = [
            'parent_id' => array('neq',0),
            'is_delete' => 0
        ];
        $field_mall = [
            'id',
            'name'
        ];
        $mall = Db::name("mall")->where($where)->field($field_mall)->select();
        if(!empty($mall)){
            $where_category = [
                'is_delete' => 0,
                'parent_id' => $mall[0]['id']
            ];
            $field = [
                'id',
                'name'
            ];
            $category = Db::name("category")->where($where_category)->field($field)->select();
            $data['mall'] = $mall;
            $data['category'] = $category;
            //print_r($data);die;
            return format_return(1,$data,"数据获取成功");
        }else{
            return format_return(0,'','数据为空');
        }
    }
    /**
    分类页面获取分类
     * @param id 分区id
     */
    public function get_category($id = 0){
        empty($id) && $this->error("非法参数");
        $where_category = [
            'is_delete' => 0,
            'parent_id' => $id,
        ];
        $field = [
            'name',
            'id'
        ];
        $category = Db::name("category")->where($where_category)->field($field)->select();
        if(!empty($category)){
            return format_return(1,$category,"数据获取成功");
        }else{
            return format_return(0,'',"数据为空");
        }
    }
    /**
    跳转品牌
     * @param id
     */
    public function jmp_brand($id = 0){
        empty($id) && $this->error("非法参数");
        $where = [
            'is_delete' => 0,
            'id' => $id
        ];
        $category = Db::name("category")->where($where)->find();
        if(!empty($category)){
//            $where['id'] = $category['parent_id'];
//            $mall = Db::name("mall")->where($where)->find();
            unset($where['id']);
            $where['parent_id'] = $category['parent_id'];
            $fir = [
                'id',
                'name'
            ];
            $cates = Db::name("category")->where($where)->field($fir)->select();
            $ids = array_column($cates,'id');
            $where_barand = [
                'c_id' => array('in',$ids),
                'm_id' => $category['parent_id'],
                'is_delete' => 0
            ];
            $field = [
                'name',
                'logo',
                'id',
                'c_id'
            ];
            $brand = Db::name("brand")->where($where_barand)->field($field)->select();
          //  print_r(sizeof($cates));die;
            for($i = 0; $i < sizeof($cates); $i ++){
                $cates[$i]['brand'] = '';
                for($j = 0; $j < sizeof($brand); $j ++){
                    if($cates[$i]['id'] == $brand[$j]['c_id']){
                        $cates[$i]['brand'][] = $brand[$j];
                    }
                }
            }
           // print_r($cates);die;
        return format_return(1,$cates,'数据获取成功');
        }else{
            return format_return(0,'','数据为空');
        }
    }
    /**
    点击品牌获取商品
     * @param id
     * @param page
     * @param condition 0 默认 1 销量 2 价格 3 时间
     * @param sort 0 升序 1 降序
     */
    public function jmp_goods($id = 0,$page = 0,$condition = 0,$sort = 0){
        $start = $page*10;
        empty($id) && $this->error("非法参数");
        $where_goods = [
            'is_delete' => 0,
            'l_id' => $id,
            'status' => 1
        ];
        $field = [
            'subject',
            'id',
            'cover',
            'price',
            'integral',
            'introduction'
        ];
        $order = '';
        switch($condition){
            case 0:
                $order = 'sell_num';
                break;
            case 1:
                $order = 'sell_num';
                break;
            case 2:
                $order = 'price';
                break;
            case 3:
                $order = 'shelves_timestamp';
                break;
            default :
                break;
        }
        switch($sort){
            case 0:
                $order .= ' asc';
                break;
            case 1 :
                $order .= ' desc';
                break;
            default :
                break;
        }
        $goods = Db::name("goods")->where($where_goods)->field($field)->order($order)->limit($start,10)->select();
        if(!empty($goods)){
            $brand = Db::name("list")->where('id',$id)->field('list_name')->find();
            $data['goods'] = $goods;
            $data['brand'] = $brand['list_name'];
            //print_r($data);die;
            return format_return(1,$data,"数据获取成功");
        }else{
            return format_return(0,'','数据为空');
        }

    }
    /**
    分类页面获取商品
     * @param id 分类id
     */
    public function get_goods($id = 0){
        empty($id) && $this->error("非法参数");
        $where_goods = [
            'is_delete' => 0,
            'c_id' => $id,
            'status' => 1
        ];
        $field = [
            'subject',
            'id'
        ];
        $goods = Db::name("goods")->where($where_goods)->field($field)->limit(15)->select();
        return format_return(1,$goods,"数据获取成功");
    }

    /**
    分类搜索页面
     * @param search 查询关键词
     * @param page 分页
     * @param condition 0 默认 1 销量 2 价格 3 时间
     * @param sort 0 升序 1 降序
     */
    public function category_search($search = '',$page = 0,$condition = 0,$sort = 0){
        $start = $page*10;
        $where = [
            'is_delete' => 0,
            'brand|subject|introduction' => ['like','%'.$search.'%'],
            'status' => 1
        ];
        $order = '';
        switch($condition){
            case 0:
                $order = 'sell_num';
                break;
            case 1:
                $order = 'sell_num';
                break;
            case 2:
                $order = 'price';
                break;
            case 3:
                $order = 'shelves_timestamp';
                break;
            default :
                break;
        }
        switch($sort){
            case 0:
                $order .= ' asc';
                break;
            case 1 :
                $order .= ' desc';
                break;
            default :
                break;
        }
        $goods = Db::name("goods")->where($where)->limit($start,10)->order($order)->select();
        if(!empty($goods)){
            $data['goods'] = $goods;
            $data['search'] = $search;
            return format_return(1,$data,"获取数据成功");
        }else{
            $goods = '';
            return format_return(0,$goods,"数据为空");
        }
    }

    /**
    商铺
     * @param s_id 商铺id
     * @param page 分页
     */
    public function store_online($s_id = 0,$page = 0){
        empty($s_id) && $this->error("非法参数");
        $start = 10*$page;
        $where = [
            'id' => $s_id,
            'status' => 1
        ];
        $field = [
            'id',
            'uid',
            'img',
            'name',
            'g_id'
        ];
        $store = Db::name("store_online")->where($where)->field($field)->find();
        $field_goods = [
            'id',
            'cover',
            'subject',
            'introduction',
            'integral',
            'price'
        ];
        $where_goods = [
            'is_delete' => 0,
            's_id' => $s_id,
            'status' => 1
        ];
        $goods = Db::name("goods")->where($where_goods)->limit($start,10)->field($field_goods)->select();
        if(!empty($goods)){
            $data['store'] = $store;
            $data['goods'] = $goods;
            return format_return(1,$data,"获取数据成功");
        }else{
            return format_return(0,'','数据为空');
        }
    }

    /**
    名牌宣传条状图
     * @param m_id 商城id
     * @param page 分页
     */
    public function propagate($m_id = 0,$page = 0){
        empty($m_id) && $this->error("非法参数");
        $where = [
            'type' => 2,
            'is_delete' => 0,
            'm_id' => $m_id,
            'is_sky' => 0
        ];
        $start = 10*$page;
        $img = Db::name("mall_index")->where($where)->limit($start,10)->select();
        return format_return(1,$img,"获取数据成功");
    }
    /**
    医美商城搜索
     * $search 搜索字符串
     * $page 分页
     * @param condition 0 默认 1 销量 2 价格 3 时间
     * @param sort 0 升序 1 降序
     */
    public function medical_search($search = '',$page = 0,$condition = 0,$sort = 0){
        $where['subject|brand'] = ['like', '%'.$search.'%'];
        $where['is_delete'] = 0;
        $where['m_id'] = 1;
        $where['status'] = 1;
        $start = $page*10;
        $order = '';
        switch($condition){
            case 0:
                $order = 'sell_num';
                break;
            case 1:
                $order = 'sell_num';
                break;
            case 2:
                $order = 'price';
                break;
            case 3:
                $order = 'shelves_timestamp';
                break;
            default :
                break;
        }
        switch($sort){
            case 0:
                $order .= ' asc';
                break;
            case 1 :
                $order .= ' desc';
                break;
            default :
                break;
        }
        $goods = Db::name("goods")->where($where)->limit($start,10)->order($order)->select();
        if(!empty($goods)){
            $data['goods'] = $goods;
            $data['search'] = $search;
            return format_return(1,$data,"获取数据成功");
        }else{
            $goods = [];
            return format_return(0,$goods,"数据为空");
        }
    }
    /**
    名品商城
     * $search 搜索字符串
     * $page 分页
     * @param condition 0 默认 1 销量 2 价格 3 时间
     * @param sort 0 升序 1 降序
     */
    public function goods_search($search = '',$page = 0,$condition = 0,$sort = 0){
        $where = [];
        $where['subject|brand'] = ['like', '%'.$search.'%'];
        $where['is_delete'] = 0;
        $where['m_id'] = 2;
        $where['status'] = 1;
        $start = $page*10;
        $order = '';
        switch($condition){
            case 0:
                $order = 'sell_num';
                break;
            case 1:
                $order = 'sell_num';
                break;
            case 2:
                $order = 'price';
                break;
            case 3:
                $order = 'shelves_timestamp';
                break;
            default :
                break;
        }
        switch($sort){
            case 0:
                $order .= ' asc';
                break;
            case 1 :
                $order .= ' desc';
                break;
            default :
                break;
        }
        $goods = Db::name("goods")->where($where)->limit($start,10)->order($order)->select();
        if(!empty($goods)){
            $data['goods'] = $goods;
            $data['search'] = $search;
            return format_return(1,$data,"获取数据成功");
        }else{
            $goods = [];
            return format_return(0,$goods,"数据为空");
        }
    }

    /**
    获取首图
     */
    public function get_index_pic(){
        $img = Db::name("index_img")->select();
        if(!empty($img)){
            return format_return(1,$img,"数据获取成功");
        }else{
            return format_return(0,'','数据获取失败');
        }
    }

    /**
    测试传图
     */
    public function insert_pic($str = ''){
        header('Access-Control-Allow-Origin:*');
        $res = Db::name("log")->insertGetId(['name'=>$str]);
        Db::name("log")->insert(['name'=>$res]);
        return json_format_return(1,$res,'数据获取成功');
//        $base64 = I('post.str');
//        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)) {
//            $type = $result[2];
//            $new_file = "./Uploads/" . time() . ".{$type}";
//            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64)))) {
//                echo '新文件保存成功：', $new_file;
//            }
//        }
    }
    /**
    获取商品列表
     */
    public function goodslib($l_id = 0){
        $goods = Db::name('goods')->where(array('l_id'=>$l_id))->select();
        if(!empty($goods)){
            return format_return(1,$goods,"数据获取成功");
        }else{
            return format_return(0,'','数据获取失败');
        }
    }
    /**
    商城分类
     * 
     */
    public function liss() {
        $ids = [5,8,9,10,11,12,13];
        $where = [
            'id' => array('in', $ids),
            'is_delete' => 0
        ];
        $goods = Db::name('mall')->where($where)->field('id,name')->select();
//        $num = count($goods);
//        $goods[$num]['name'] = "热门推荐";
//        $goods = array_reverse($goods);
//        $goods[0]['children'] = [''];
        for ($i = 0; $i < count($goods); $i++) {
            $goods[$i]['children'] = Db::name('category')->where(array('parent_id' => $goods[$i]['id'],'is_delete'=>0))->field('id,name')->select();
            for($j=0;$j<count($goods[$i]['children']);$j++){
                $goods[$i]['children'][$j]['child'] = Db::name('list')->where(array('c_id'=>$goods[$i]['children'][$j]['id'],'is_delete'=>0))->field('id,list_name,img')->select();
            }
        }
//        dump($goods);die;
        if(!empty($goods)){
            return format_return(1,$goods,"数据获取成功");
        }else{
            return format_return(0,'','数据获取失败');
        }
    }
}
